/* player.c

 *
 *  Created on: 15 nov. 2010
 *      Author: Nicolas Aucouturier, Laurent Reveillere
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include <time.h>
#include "SDL.h"
#include "SDL_image.h"

#include "../include/game.h"
#include "../include/misc.h"

struct s_player {
	int x, y;
	SDL_Surface * directed_img[4];
	enum e_way current_way;
	int key;
	/*int init_x;
	int init_y;*/


	int monster_or_not;

	int numb_map; //The number of the current map of the player
	int var_map; //To manage the change of map of the player
	int numb_level;
	int numb_level_old;

	int nb_bomb;
	int nb_life;
	int bomb_range;
};

t_player player_init(int bomb_number,int life,int var,int init_x,int init_y,int num_map) {
	t_player player = malloc(sizeof(*player));
	if (!player)
		error("Memory error");

	player->directed_img[WEST] = load_image(IMG_PLAYER_LEFT);
	player->directed_img[EAST] = load_image(IMG_PLAYER_RIGHT);
	player->directed_img[NORTH] = load_image(IMG_PLAYER_UP);
	player->directed_img[SOUTH] = load_image(IMG_PLAYER_DOWN);

	player->current_way = SOUTH;
	player->nb_bomb = bomb_number;
	player->nb_life=life;
	player->key=0;
	player->bomb_range=2;
	player->numb_map=num_map;
	player->var_map=-1;


	player->x=init_x;
	player->y=init_y;

	if(var==0)
		{

			player->numb_level_old=1;
			player->numb_level=1;
		}
	else
	{

		player->numb_level_old=var;
		player->numb_level=var;
	}
	assert(player);
	return player;
}


t_player player_init_last(int bomb_number,int life,int var) {
	t_player player = malloc(sizeof(*player));
	if (!player)
		error("Memory error");

	/*player->directed_img[WEST] = load_image(IMG_PLAYER_LEFT);
	player->directed_img[EAST] = load_image(IMG_PLAYER_RIGHT);
	player->directed_img[NORTH] = load_image(IMG_PLAYER_UP);
	player->directed_img[SOUTH] = load_image(IMG_PLAYER_DOWN);*/

	player->current_way = SOUTH;
	player->nb_bomb = bomb_number;
	player->nb_life=life;
	player->key=0;
	player->bomb_range=2;
	player->numb_map=-1;


	player->x=0;
	player->y=0;

	if(var==0)
		{

			player->numb_level_old=1;
			player->numb_level=1;
		}
	else
	{

		player->numb_level_old=var;
		player->numb_level=var;
	}
	assert(player);
	return player;
}





int player_get_key(t_player player)
{
	return player->key;
}

void inc_player_key(t_player player)
{
	player->key=player->key+1;
}

void player_free(t_player player) {
	assert(player);
	int i;
	for (i = 0; i < 4; i++)
		SDL_FreeSurface(player->directed_img[i]);

	free(player);
}

int player_get_x(t_player player) {
	assert(player != NULL);
	return player->x;
}

int player_get_y(t_player player) {
	assert(player != NULL);
	return player->y;
}


void player_set_current_way(t_player player, enum e_way way) {
	assert(player);
	player->current_way = way;
}

int player_get_bomb_range(t_player player) {
	//assert(player != NULL);
	return player->bomb_range;
}

void player_inc_bomb_range(t_player player) {
	//assert(player);
	player->bomb_range += 1;
}

void player_set_nb_bomb(t_player player,int x){
	player->nb_bomb=x;
}

void player_set_bomb_range(t_player player,int x) {

	player->bomb_range =x;
}

void player_dec_bomb_range(t_player player) {
	assert(player);
	if (player_get_bomb_range(player) > 0)
		player->bomb_range -= 1;
}

void player_inc_nb_life(t_player player) {
	assert(player);
	if (player->nb_life < 9) {
		player->nb_life += 1;
	}
}

void player_dec_nb_life(t_player player) {
	assert(player);
		player->nb_life -= 1;  

}


int player_get_nb_life(t_player player) {
	assert(player);
	return player->nb_life;
}

int player_get_nb_bomb(t_player player) {
	assert(player);
	return player->nb_bomb;
}

void player_inc_nb_bomb(t_player player) {
	assert(player);
	player->nb_bomb += 1;
}

void player_dec_nb_bomb(t_player player) {
	assert(player);
	if (player_get_nb_bomb(player) > 0)
		player->nb_bomb -= 1;
}

void player_from_map(t_player player, t_map map) {
	int i, j;
	for (i = 0; i < map_get_width(map); i++) {
		for (j = 0; j < map_get_height(map); j++) {
			if (map_get_cell_type(map, i, j) == CELL_PLAYER) {

				player->x = i;
				player->y = j;

			}
		}
	}
}

static int player_move_aux(t_player player, t_map map, int x1, int y1, int x2,
		int y2) {

	player->monster_or_not=0;

	if (!map_is_inside(map, x1, y1))
		return 0;

	switch (map_get_cell_type(map, x1, y1)) {

	case CELL_DOOR:
		if(map_get_door_state(map, x1, y1)==0 || player_get_key(player)!=0)
		{
			return 1;}
		else
			return 0;

		break;

	case CELL_KEY:
	  inc_player_key(player);
		break;

	case CELL_SCENERY:
		return 0;
		break;

	case CELL_CASE:
		if(/*map_get_cell_type(map, x2, y2)==CELL_EMPTY && x2>=0 && x2<=11 && y2>=0 && y2<=11*/map_is_inside(map,x2,y2) && map_get_cell_type(map, x2, y2)==CELL_EMPTY)
			{map_set_cell_type(map,x2,y2,CELL_CASE);
			return 1;}
		else
			return 0;
		//return 0;
		break;

	case CELL_BONUS:
		bonus_management(map,player,x1,y1);
		break;


	case CELL_MONSTER:
		player_dec_nb_life(player);
		player->monster_or_not=1;
		break;
      
  case CELL_GOAL:
		player->numb_level=player->numb_level+1;
		player->numb_level_old=player->numb_level-1;
		//printf("%d,%d",player->numb_level,player->numb_level_old);
		break;

  case CELL_BOMBERWOMAN:
	  player->numb_level=player->numb_level+1;
	  player->numb_level_old=player->numb_level-1;
	  //printf("%d,%d",player->numb_level,player->numb_level_old);
	  break;

	case CELL_PLAYER:
		break;

	case CELL_BOMB:
		return 0;
		break;

	default:
		break;
	}

	// Player has moved
	return 1;
}

int player_get_monster_or_not(t_player player)
{
	return player->monster_or_not;
}

int player_move(t_player player, t_map map) {
	int x = player->x;

	int y = player->y;

	int move = 0;

	switch (player->current_way) {
	case NORTH:
		if (player_move_aux(player, map, x, y - 1, x, y - 2) /*&& player->y>=1*/ ) {
			player->y--;
			move = 1;
		}
		break;

	case SOUTH:
		if (player_move_aux(player, map, x, y + 1, x, y + 2) /*&& player->y<=10*/) {
			player->y++;
			move = 1;
		}
		break;

	case WEST:
		if (player_move_aux(player, map, x - 1, y, x - 2, y) /*&& player->x>=1*/) {
			player->x--;
			move = 1;
		}
		break;

	case EAST:
		if (player_move_aux(player, map, x + 1, y, x + 2, y) /*&& player->x<=10*/) {
			player->x++;
			move = 1;
		}
		break;
	}

	if (move) {
		if(map_get_cell_type(map, x, y)!=CELL_BOMB && map_get_cell_type(map, x,y)!=CELL_DOOR)								//Pour que quand on quitte une cas de type bomb le type devienne empty et que l'on puisse par la suite marcher sur la bomb
				map_set_cell_type(map, x, y, CELL_EMPTY);
		if(map_get_cell_type(map, player_get_x(player),player_get_y(player))!=CELL_MONSTER && map_get_cell_type(map, player_get_x(player),player_get_y(player))!=CELL_DOOR)
			map_set_cell_type(map, player->x, player->y, CELL_PLAYER);

		//If there is a door, we check the map where it will bring the player
		if(map_get_cell_type(map, player_get_x(player),player_get_y(player))==CELL_DOOR)
		{
			int j=-1;
			j=map_get_door_numb(map,player_get_x(player),player_get_y(player));
			printf("%d",j);
			player->numb_map=map_get_door_numb(map,player_get_x(player),player_get_y(player));
			player_set_var_map(player,j);

		}

	}
	return move;
}

void player_set_var_map(t_player player,int x)
{
	player->var_map=x;
}

void player_set_old_level(t_player player,int x)
{
	player->numb_level_old=x;
}
void reinitialise_player_var_map(t_player player)
{
	player->var_map=-1;
}
int player_get_var_map(t_player player)
{
	return player->var_map;
}

int player_get_numb_map(t_player player)
{
	return player->numb_map;
}

int player_get_numb_level(t_player player)
{
	return player->numb_level;
}

int player_get_numb_level_old(t_player player)
{
	return player->numb_level_old;
}

void inc_numb_level_old(t_player player)
{
	player->numb_level_old=player->numb_level_old+1;
}


void player_display(t_player player, SDL_Surface *screen) {
	assert(player);

	assert(player->directed_img[player->current_way]);
	assert(screen);

	SDL_Rect place;
	place.x = player->x * SIZE_BLOC;
	place.y = player->y * SIZE_BLOC;

	SDL_BlitSurface(player->directed_img[player->current_way], NULL, screen,
			&place);
}


void bonus_management(t_map map,t_player player,int x,int y){

	//t_cell_type bonus_type=map_get_cell_type2(map,x,y);

	switch (/*bonus_type*/map_get_cell_type2(map,x,y)) {
	case 1/*CELL_BONUS_BOMBINC*/:
		player_inc_bomb_range(player);
		//player_inc_nb_bomb(player);
		break;
	case 2/*CELL_BONUS_BOMBDEC*/:
		if(player->bomb_range>0)
			player_dec_bomb_range(player);
		//player_dec_nb_bomb(player);
		break;
	case 3/*CELL_BONUS_RANGEINC*/:
		player_inc_nb_bomb(player);
		//player_inc_bomb_range(player);
		break;
	case 4:/*CELL_CASE_RANGEDEC:*/
		player_dec_nb_bomb(player);
		//player_dec_bomb_range(player);
		break;
	case 5/*CELL_CASE_MONSTER:*/:
		player_inc_nb_life(player);
		//map_set_cell_type(map,x,y,CELL_EMPTY);
		break;
	/*case CELL_CASE:
		map_set_cell_type(map,x,y,CELL_EMPTY);
		break;*/
	default:
		break;
}
}



